<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <ui:composition template="../../templates/master.xhtml">
            <ui:define name="content">
                    
                <p:dataTable value="#{postsController.posts}" paginator="true" rows="10"
                             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
                             var="c" rowsPerPageTemplate="5,10,15" selectionMode="single" id="postTable" lazy="true">
                    <p:column styleClass="row-item-#{c.id}" headerText="ID">
                        <h:outputText value="#{c.id}"/>
                    </p:column>
                    <p:column sortBy="#{c.name}" filterBy="#{c.name}" headerText="Name">
                        <h:outputText value="#{c.name}"/>
                    </p:column>
                    <p:column headerText="Date Created">
                        <h:outputText value="#{c.dateCreated}">
                            <f:convertDateTime pattern="dd-MM-yyyy"/>
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Last Updated">
                        <h:outputText value="#{c.lastUpdate}">
                            <f:convertDateTime pattern="dd-MM-yyyy"/>
                        </h:outputText>
                    </p:column>
                    <p:column headerText="#">
                        <!--                        <p:commandButton id="ViewDetailButton" title="View" icon="ui-icon-search">
                                                    <f:setPropertyActionListener value="#{c}" target="#{postsController.selected}"/>
                                                </p:commandButton>-->
                        <p:commandButton id="ViewButton" action="#{postsController.prepareView()}" title="View" icon="ui-icon-search">
                            <f:setPropertyActionListener value="#{c}" target="#{postsController.selected}"/>
                        </p:commandButton>
                        <p:commandButton id="EditButton" action="#{postsController.prepareEdit()}" title="Edit" icon="ui-icon-edit">
                            <f:setPropertyActionListener value="#{c}" target="#{postsController.selected}"/>
                        </p:commandButton>
                        <p:commandButton oncomplete="onDeleteRowPostComplete(xhr,status,args)" id="DeleteButton" title="Delete" action="#{postsController.destroy()}" icon="ui-icon-close">
                            <f:setPropertyActionListener value="#{c}" target="#{postsController.selected}"/>
                        </p:commandButton>
                    </p:column>
                </p:dataTable>
                <p:commandButton value="Create" action="#{postsController.prepareCreate()}"/>
            </ui:define>
            <ui:define name="footer">
                <ui:include src="../../templates/footer.xhtml"/>
                <script type="text/javascript">
                    function onDeleteRowPostComplete(xhr, status,args){
                        var item = args.item;
                        $("td.row-item-"+item).closest("tr").fadeOut("fast").remove();
                        console.log(item);
                    }
                </script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

